﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

namespace HYFloorPipeModule
{
	public class HeaderUnitView : DataGridView
	{
		public int CellHeight
		{
			get
			{
				return this._cellHeight;
			}
			set
			{
				this._cellHeight = value;
			}
		}

		[Description("水平滚动时是否刷新表头，数据较多时可能会闪烁，不刷新时可能显示错误")]
		public bool RefreshAtHscroll
		{
			get
			{
				return this.HscrollRefresh;
			}
			set
			{
				this.HscrollRefresh = value;
			}
		}

		public HeaderUnitView()
		{
			this.InitializeComponent();
			base.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
		}

		public HeaderUnitView(IContainer container)
		{
			container.Add(this);
			this.InitializeComponent();
		}

		[Description("设置或获得合并表头树的深度")]
		public int ColumnDeep
		{
			get
			{
				if (base.Columns.Count == 0)
				{
					this._columnDeep = 1;
				}
				base.ColumnHeadersHeight = this._cellHeight * this._columnDeep;
				return this._columnDeep;
			}
			set
			{
				if (value < 1)
				{
					this._columnDeep = 1;
				}
				else
				{
					this._columnDeep = value;
				}
				base.ColumnHeadersHeight = this._cellHeight * this._columnDeep;
			}
		}

		[Description("添加合并式单元格绘制的所需要的节点对象")]
		public TreeView[] ColumnTreeView
		{
			get
			{
				return this._columnTreeView;
			}
			set
			{
				if (this._columnTreeView != null)
				{
					for (int i = 0; i <= this._columnTreeView.Length - 1; i++)
					{
						this._columnTreeView[i].Dispose();
					}
				}
				this._columnTreeView = value;
			}
		}

		[Description("设置添加的字段树的相关属性")]
		public TreeView ColumnTreeViewNode
		{
			get
			{
				return this._columnTreeView[0];
			}
		}

		[MergableProperty(false)]
		[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Localizable(true)]
		[Description("设置或获取合并列的集合")]
		[Browsable(true)]
		[Category("单元格合并")]
		public List<string> MergeColumnNames
		{
			get
			{
				return this._mergecolumnname;
			}
			set
			{
				this._mergecolumnname = value;
			}
		}

		public ArrayList NadirColumnList
		{
			get
			{
				if (this._columnTreeView == null)
				{
					return null;
				}
				if (this._columnTreeView[0] == null)
				{
					return null;
				}
				if (this._columnTreeView[0].Nodes == null)
				{
					return null;
				}
				if (this._columnTreeView[0].Nodes.Count == 0)
				{
					return null;
				}
				this._columnList.Clear();
				this.GetNadirColumnNodes(this._columnList, this._columnTreeView[0].Nodes[0], false);
				return this._columnList;
			}
		}

		public void PaintUnitHeader(TreeNode node, DataGridViewCellPaintingEventArgs e, int level)
		{
			if (level == 0)
			{
				return;
			}
			Brush brush = new SolidBrush(base.GridColor);
			SolidBrush brush2 = new SolidBrush(e.CellStyle.BackColor);
			Pen pen = new Pen(brush);
			new StringFormat().Alignment = StringAlignment.Center;
			int unitHeaderWidth = this.GetUnitHeaderWidth(node);
			RectangleF rect;
			if (node.Nodes.Count == 0)
			{
				rect = new System.Drawing.Rectangle(e.CellBounds.Left, e.CellBounds.Top + node.Level * this._cellHeight, unitHeaderWidth - 1, this._cellHeight * (this._columnDeep - node.Level) - 1);
			}
			else
			{
				rect = new System.Drawing.Rectangle(e.CellBounds.Left, e.CellBounds.Top + node.Level * this._cellHeight, unitHeaderWidth - 1, this._cellHeight - 1);
			}
			e.Graphics.FillRectangle(brush2, rect);
			e.Graphics.DrawLine(pen, rect.Left, rect.Bottom, rect.Right, rect.Bottom);
			e.Graphics.DrawLine(pen, rect.Right, rect.Top, rect.Right, rect.Bottom);
			e.Graphics.DrawString(node.Text, this.Font, new SolidBrush(e.CellStyle.ForeColor), rect.Left + rect.Width / 2f - e.Graphics.MeasureString(node.Text, this.Font).Width / 2f - 1f, rect.Top + rect.Height / 2f - e.Graphics.MeasureString(node.Text, this.Font).Height / 2f);
			if (node.PrevNode == null && node.Parent != null)
			{
				this.PaintUnitHeader(node.Parent, e, level - 1);
			}
		}

		private int GetUnitHeaderWidth(TreeNode node)
		{
			int num = 0;
			if (node.Nodes == null)
			{
				return base.Columns[this.GetColumnListNodeIndex(node)].Width;
			}
			if (node.Nodes.Count == 0)
			{
				return base.Columns[this.GetColumnListNodeIndex(node)].Width;
			}
			for (int i = 0; i <= node.Nodes.Count - 1; i++)
			{
				num += this.GetUnitHeaderWidth(node.Nodes[i]);
			}
			return num;
		}

		private int GetColumnListNodeIndex(TreeNode node)
		{
			for (int i = 0; i <= this._columnList.Count - 1; i++)
			{
				if (((TreeNode)this._columnList[i]).Equals(node))
				{
					return i;
				}
			}
			return -1;
		}

		private void GetNadirColumnNodes(ArrayList alList, TreeNode node, bool isChecked)
		{
			if (!isChecked)
			{
				if (node.FirstNode == null)
				{
					alList.Add(node);
					if (node.NextNode != null)
					{
						this.GetNadirColumnNodes(alList, node.NextNode, false);
						return;
					}
					if (node.Parent != null)
					{
						this.GetNadirColumnNodes(alList, node.Parent, true);
						return;
					}
				}
				else if (node.FirstNode != null)
				{
					this.GetNadirColumnNodes(alList, node.FirstNode, false);
					return;
				}
			}
			else
			{
				if (node.FirstNode == null)
				{
					return;
				}
				if (node.NextNode != null)
				{
					this.GetNadirColumnNodes(alList, node.NextNode, false);
					return;
				}
				if (node.Parent != null)
				{
					this.GetNadirColumnNodes(alList, node.Parent, true);
					return;
				}
			}
		}

		protected override void OnScroll(ScrollEventArgs e)
		{
			bool flag = e.ScrollOrientation == ScrollOrientation.HorizontalScroll;
			base.OnScroll(e);
			if (this.RefreshAtHscroll && flag)
			{
				this.Refresh();
			}
		}

		protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)
		{
			float width = Graphics.FromHwnd(base.Handle).MeasureString(e.Column.HeaderText, this.Font).Width;
			if (width >= (float)e.Column.Width)
			{
				e.Column.Width = (int)Convert.ToInt16(width);
			}
			base.OnColumnWidthChanged(e);
		}

		protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
		{
			try
			{
				if (e.RowIndex > -1 && e.ColumnIndex > -1)
				{
					this.DrawCell(e);
				}
				else if (e.ColumnIndex < 0)
				{
					base.OnCellPainting(e);
				}
				else if (this._columnDeep == 1)
				{
					base.OnCellPainting(e);
				}
				else if (e.RowIndex == -1)
				{
					if (e.ColumnIndex >= this.NadirColumnList.Count)
					{
						e.Handled = true;
					}
					else
					{
						this.PaintUnitHeader((TreeNode)this.NadirColumnList[e.ColumnIndex], e, this._columnDeep);
						e.Handled = true;
					}
				}
			}
			catch
			{
			}
		}

		private void DrawCell(DataGridViewCellPaintingEventArgs e)
		{
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet)
			{
				e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
			}
			Brush brush = new SolidBrush(base.GridColor);
			SolidBrush solidBrush = new SolidBrush(e.CellStyle.BackColor);
			SolidBrush solidBrush2 = new SolidBrush(e.CellStyle.ForeColor);
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			if (this.MergeColumnNames.Contains(base.Columns[e.ColumnIndex].Name) && e.RowIndex != -1)
			{
				int num4 = e.CellBounds.Width;
				Pen pen = new Pen(brush);
				string value = (e.Value == null) ? "" : e.Value.ToString().Trim();
				if (base.CurrentRow.Cells[e.ColumnIndex].Value != null)
				{
					base.CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim();
				}
				if (!string.IsNullOrEmpty(value))
				{
					int num5 = e.RowIndex;
					while (num5 < base.Rows.Count && base.Rows[num5].Cells[e.ColumnIndex].Value.ToString().Equals(value))
					{
						num2++;
						if (e.RowIndex != num5)
						{
							num4 = ((num4 < base.Rows[num5].Cells[e.ColumnIndex].Size.Width) ? num4 : base.Rows[num5].Cells[e.ColumnIndex].Size.Width);
						}
						num5++;
					}
					int num6 = e.RowIndex;
					while (num6 >= 0 && base.Rows[num6].Cells[e.ColumnIndex].Value.ToString().Equals(value))
					{
						num++;
						if (e.RowIndex != num6)
						{
							num4 = ((num4 < base.Rows[num6].Cells[e.ColumnIndex].Size.Width) ? num4 : base.Rows[num6].Cells[e.ColumnIndex].Size.Width);
						}
						num6--;
					}
					num3 = num2 + num - 1;
					if (num3 < 2)
					{
						return;
					}
				}
				if (base.Rows[e.RowIndex].Selected)
				{
					solidBrush.Color = e.CellStyle.SelectionBackColor;
					solidBrush2.Color = e.CellStyle.SelectionForeColor;
				}
				e.Graphics.FillRectangle(solidBrush, e.CellBounds);
				this.PaintingFont(e, num4, num, num2, num3);
				if (num2 == 1)
				{
					e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
				}
				e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
				e.Handled = true;
			}
		}

		private void PaintingFont(DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
		{
			SolidBrush brush = new SolidBrush(e.CellStyle.ForeColor);
			int num = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
			int num2 = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
			int height = e.CellBounds.Height;
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)(e.CellBounds.X + (cellwidth - num2) / 2), (float)(e.CellBounds.Y + height * DownRows - num));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)e.CellBounds.X, (float)(e.CellBounds.Y + height * DownRows - num));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)(e.CellBounds.X + cellwidth - num2), (float)(e.CellBounds.Y + height * DownRows - num));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)(e.CellBounds.X + (cellwidth - num2) / 2), (float)(e.CellBounds.Y - height * (UpRows - 1) + (height * count - num) / 2));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)e.CellBounds.X, (float)(e.CellBounds.Y - height * (UpRows - 1) + (height * count - num) / 2));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)(e.CellBounds.X + cellwidth - num2), (float)(e.CellBounds.Y - height * (UpRows - 1) + (height * count - num) / 2));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)(e.CellBounds.X + (cellwidth - num2) / 2), (float)(e.CellBounds.Y - height * (UpRows - 1)));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)e.CellBounds.X, (float)(e.CellBounds.Y - height * (UpRows - 1)));
				return;
			}
			if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
			{
				e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)(e.CellBounds.X + cellwidth - num2), (float)(e.CellBounds.Y - height * (UpRows - 1)));
				return;
			}
			e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, brush, (float)(e.CellBounds.X + (cellwidth - num2) / 2), (float)(e.CellBounds.Y - height * (UpRows - 1) + (height * count - num) / 2));
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.components = new Container();
		}

		private TreeView[] _columnTreeView;

		private ArrayList _columnList = new ArrayList();

		private int _cellHeight = 17;

		private int _columnDeep = 1;

		private bool HscrollRefresh;

		private List<string> _mergecolumnname = new List<string>();

		private IContainer components;
	}
}
